package com.cytech.cloudclinic.moudle.main.clinic;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.ClinicAdapter;
import com.cytech.cloudclinic.base.BaseFragment;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.bean.event.TollEvent;
import com.cytech.cloudclinic.moudle.login.LoginActivity;
import com.cytech.cloudclinic.moudle.main.clinic.admission.AdmissionActivity;
import com.cytech.cloudclinic.moudle.main.patient.add.AddPatientActivity;
import com.yechaoa.yutils.SpUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.cytech.cloudclinic.app.Constant.USER_TOKEN;

public class ClinicFragment extends BaseFragment<ClinicPresenter> implements ClinicView {
    @BindView(R.id.rv_clinic_main)
    RecyclerView recyclerView;
    @BindView(R.id.tv_clinic_phone_num)
    TextView phoneTv;

    private List<Patient> mList;
    private ClinicAdapter mAdapater;

    public static ClinicFragment newInstance() {
        ClinicFragment fragment = new ClinicFragment();
        //如果有参数，使用Bundle传递
        return fragment;
    }


    @Override
    protected ClinicPresenter createPresenter() {
        return new ClinicPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_clinic;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        phoneTv.setText(SpUtil.getString(SpUtil.getString(USER_TOKEN)));
    }

    @Override
    protected void initData() {
        mList = new ArrayList<>();
        mAdapater = new ClinicAdapter(mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(mAdapater);

        mAdapater.addChildClickViewIds(R.id.tv_item_clinic_patient_action);
        mAdapater.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                AdmissionActivity.actionStart(getActivity(),mAdapater.getItem(position));
            }
        });


        presenter.getPatientList();
    }

    @OnClick(R.id.ll_add_patient)
    public void addPatient(View view){
        AddPatientActivity.actionStart(getActivity());
    }

    @OnClick(R.id.tv_clinic_exit)
    public void exitAccount(View view){
        SpUtil.removeByKey(USER_TOKEN);
        LoginActivity.actionStart(getActivity());
        getActivity().finish();
    }

    @Override
    public void getPatientList(List<Patient> data) {
        mAdapater.setNewInstance(data);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void tollSuccessEvent(TollEvent event){
        presenter.getPatientList();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
